package com.antfinancial.antchain.smallprogram.service;

import com.alipay.mychain.gl.biz.common.Response;
import com.alipay.mychain.gl.biz.sdk.Client;
import com.alipay.mychain.gl.common.crypto.Hex;
import com.alipay.mychain.gl.common.model.builder.TransactionBuilder;
import com.alipay.mychain.gl.common.model.entity.TransactionDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

@Service
public class BlockchainService {
    private static final Logger logger= LoggerFactory.getLogger(BlockchainService.class);
    @Value("${account_addr}")
    private String accountAddr;
    @Value("${private_key}")
    String privateKey;
    @Value("${public_key}")
    String publicKey;
    @Autowired
    private Client mychainClient;
    public String callMethod(Long id, Integer level, Timestamp createTime) throws Exception{
        String data=id+"-"+level+"-"+createTime.getTime();
        TransactionDO tx = TransactionBuilder.getContentOnlyNotaryPayloadBuilder()
                .setContent(data.getBytes())
                .setCategory(256)
                .setTimestamp(System.currentTimeMillis())
                .build();
        Response<TransactionDO> response=mychainClient.sendTransaction(tx);
        return new String(Hex.encode(response.getData().getTxHash()));
    }
//    public void queryTransaction(String txHash){
//        Map<String,Object> result=new HashMap<>();
//        final TransactionQuery transactionQuery = new TransactionQuery(txHash, false);
//        transactionQuery.buildQuery();
//        Request request = mychainClient.buildRequest(transactionQuery);
//        Response response = mychainClient.sendRequest(request, 100000);
//        final TransactionQueryReply transactionQueryReply = (TransactionQueryReply) response.getResponsePayload();
//        CallMethodTransactionDO callMethodTransactionDO = (CallMethodTransactionDO)transactionQueryReply.getTransaction();
//        result.put("result",callMethodTransactionDO.getArgs());
//    }
}
